import React from 'react'
import axios, { defaults } from 'axios'
import { Modal } from 'antd'
import gdata from 'gdata'


/**
 * @func 请求配置
 * @author wy
 */

const request = axios.create({
    baseURL: '/api/fuwu/web/',
})

request.interceptors.response.use((response) => {
    const tryUrl = ['getUser.action', 'getServiceInfo.action'] 
    const data = response.data || {}
    const url = response.config.url
    if (response.status == 200) {
        if(data.result == 'losesession'){
            window.location.href = gdata('ucDomain')+'/Contents/usercenter/allow/login/login.jsp?redirecturl=jss&jumpurl='+ encodeURIComponent(location.href);
            return Promise.reject()
        } else if(data.result == 'incompleteInformation'){
            let modal ={};
            let Domain = gdata('ucDomain') + '/usercenter/company/getCompanyInfo.do';
            modal.hide = function(){
                modal.self.destroy();
            }
            const Info = function(){
                return (
                    <div style={{paddingBottom:'30px'}}>
                        <img src={`${staticProject}static/common/images/warn.png`} width={36} style={{margin:'0'}}/>
                        <div style={{width:'378px',position:'absolute',top:'30px',left:'85px',color:'#544A38',lineHeight:'20px',fontSize:'14px',}} >
                            <div>您还未关联手机号，请先完成关联手机号设置。关联完成后，请刷新当前页面。</div>
                            <div style={{marginTop:'30px'}}><a style={{color:'#9F7724'}} href={Domain} target="_blank" onClick={modal.hide}>去设置</a></div>
                        </div>
                    </div>
                )
            }
            modal.self = Modal.info({
                centered:true,
                title:null,
                className:'pure',
                width:600,
                okButtonProps:{style:{display:'none'}},
                iconType:1,
                content:<Info />
            })
        } else if (data.result == 'fail' || data.result == 'false') {

            let flag = tryUrl.some((item) => {
                if (url.indexOf(item) > -1) {
                    return true
                }
            })
            // 情景特殊false 也相当于成功 返回data
            if (flag) {
                // 获取二级域名特殊性
                if (url.indexOf('getServiceInfo.action') > -1) {
                    return {
                            "data": {
                                    "areaCode":"330100",
                                    "areaId":"201204151213141013",
                                    "areaName":"杭州",
                                    "provinceCode":"330000",
                                    "provinceName":"浙江",
                                    "serviceId":"F7A54574338948AC6289"
                                },
                            "result":"true"
                        }
                }
                return data
            } else {
                Modal.error({                                
                    centered:true,
                    title:null,
                    content: data.message,
                }) 
                return Promise.reject()              
            }
        } else {                                
            return data
        }
    }}, (error) => {
        return Promise.reject(error);
    }                                
)

export default request


 